﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using l1cs.server.serverpackets;
using l1cs.server.model.Instance;

namespace l1cs.server.model
{
    public class L1DeleteItemOnGround
    {
        //private DeleteTimer _deleteTimer;

        private static Logger _log = Logger.getLogger( typeof( L1DeleteItemOnGround ).FullName );

        public L1DeleteItemOnGround() { }

        //private class DeleteTimer implements Runnable {
        //	public DeleteTimer() {
        //	}

        public void DeleteTimerSub( Object state )
        {
            int time = Config.ALT_ITEM_DELETION_TIME * 60 * 1000 - 10 * 1000;
            while ( true )
            {
                Thread.Sleep( time );

                L1World.getInstance().broadcastPacketToAll(
                        new S_ServerMessage( 166, "ワールドマップ上のアイテム",
                                "10秒後に削除されます。" ) ); // \f1%0が%4%1%3 %2
                Thread.Sleep( 10000 );

                deleteItem();
                L1World.getInstance().broadcastPacketToAll(
                        new S_ServerMessage( 166, "ワールドマップ上のアイテム", "削除されました。" ) ); // \f1%0が%4%1%3%2
            }
        }
        //}

        public void initialize()
        {
            if ( string.Compare( Config.ALT_ITEM_DELETION_TYPE, "auto", true ) != 0 )
            {
                return;
            }

            //_deleteTimer = new DeleteTimer();
            //GeneralThreadPool.getInstance().execute(_deleteTimer); // タイマー開始
            ThreadPool.QueueUserWorkItem( new WaitCallback( DeleteTimerSub ) );
        }

        private void deleteItem()
        {
            int numOfDeleted = 0;
            foreach ( L1Object obj in L1World.getInstance().getObject() )
            {
                if ( !( obj is L1ItemInstance ) )
                {
                    continue;
                }

                L1ItemInstance item = (L1ItemInstance)obj;
                if ( item.getX() == 0 && item.getY() == 0 )
                { // 地面上のアイテムではなく、誰かの所有物
                    continue;
                }
                if ( item.getItem().getItemId() == 40515 )
                { // 精霊の石
                    continue;
                }
                if ( L1HouseLocation.isInHouse( item.getX(), item.getY(), item
                        .getMapId() ) )
                { // アジト内
                    continue;
                }

                List<L1PcInstance> players = L1World.getInstance()
                        .getVisiblePlayer( item, Config.ALT_ITEM_DELETION_RANGE );
                if ( players.Count == 0 )
                { // 指定範囲内にプレイヤーが居なければ削除
                    L1Inventory groundInventory = L1World
                            .getInstance()
                            .getInventory( item.getX(), item.getY(), item.getMapId() );
                    groundInventory.removeItem( item );
                    numOfDeleted++;
                }
            }
            _log.fine( "ワールドマップ上のアイテムを自動削除。削除数: " + numOfDeleted );
        }
    }

}
